// pages/reservation/index.js
import dayjs from 'dayjs';
import {
    getCourse,
    reservationCourse
} from '@/services/reservation/index.js'
import {
    apiUrl
} from '@/config/index'
import {
    useDict
} from '@/utils/dict'
Page({
    data: {
        dayList: [],
        dayValue: '',
        apiUrl,
        typeList: [{
            label: '全部',
            value: ''
        }],
        statusLables: {
            1: '待预约',
            2: '暂停预约',
            4: '已满员',
            9: '停止预约'
        },
        typeValue: '',
        courseList: [],
        showWarnConfirm: false,
        currentSelect: {},
    },
    initPage() {
        const cDay = new Date().getTime();
        const list = [];
        for (let index = 0; index < 15; index++) {
            const time = cDay + (index * 86400000)
            list.push({
                ...this.getTime(time)
            })
        }
        this.setData({
            dayList: list,
            dayValue: list[0].day
        });
        this.getCourseData(list[0].day, this.data.typeValue)
        useDict('course_type').then((res) => {
            this.setData({
                typeList: [{
                        label: '全部',
                        value: ''
                    },
                    ...res['course_type']
                ]
            })
        })
    },
    getTime(time) {
        const tc = {
            0: '日',
            1: '一',
            2: '二',
            3: '三',
            4: '四',
            5: '五',
            6: '六'
        }
        const date = {
            day: '',
            week: '',
            year: ''
        }
        date.day = dayjs(time).format('MM.DD');
        date.week = `周${tc[dayjs(time).day()]}`;
        date.year = dayjs(time).format('YYYY');
        return date
    },
    onTabsChange(event) {
        const {
            item
        } = event.currentTarget.dataset
        this.setData({
            dayValue: item.day
        })
        this.getCourseData(item.day, this.data.typeValue)
    },
    onTypeChange(event) {
        const {
            item
        } = event.currentTarget.dataset
        this.setData({
            typeValue: item.value
        })
        this.getCourseData(this.data.dayValue, item.value)
    },
    getCourseData(day, type) {
        const dayNode = this.data.dayList.find(v => v.day === day)
        getCourse({
            sstartTime: `${dayNode?.year || '2025'}-${day.replace('.', '-')}`,
            courseType: type
        }).then((res) => {
            this.setData({
                courseList: res.data
            })
        })
    },
    toReservation(event) {
        this.setData({
            currentSelect: event.currentTarget.dataset.item,
            showWarnConfirm: true
        })
    },
    confirmReservation() {
        this.closeDialog()
        const userInfo = JSON.parse(wx.getStorageSync('userInfo') || '{}')
        if (userInfo && userInfo.customerId) {
            reservationCourse({
                customerId: userInfo.customerId,
                scheduleId: this.data.currentSelect.courseId,
                reservationType: 2
            }).then((res) => {
                console.log(res)
                wx.showToast({
                    title: '预约成功',
                    icon: 'success',
                    duration: 2000
                })
            })
        } else {
            wx.navigateTo({
                url: '/pages/login/login'
            })
        }
    },
    closeDialog() {
        this.setData({
            showWarnConfirm: false
        })
    },
    toDateil(event) {
        const {
            item
        } = event.currentTarget.dataset
        wx.navigateTo({
            url: '/pages/goods/details/index?id=' + item.id
        })
    },
    dictFormatter(value) {
        const obj = this.data.typeList.find(item => item.value === value)
        console.log(obj)
        return obj ? obj.label : value
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.initPage()
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },
    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})